package com.zzyl.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service()
public class DeviceDataServiceImpl implements DeviceDataService {
    @Autowired
    private DeviceDataMapper deviceDataMapper;

    @Override
    public PageResponse<DeviceDataVo> getDeviceDataPage(DeviceDataPageReqDto deviceDataPageReqDto) {

        PageHelper.startPage(deviceDataPageReqDto.getPageNum(), deviceDataPageReqDto.getPageSize());
        //
        LocalDateTime startDate = LocalDateTimeUtil.of(deviceDataPageReqDto.getStartTime());
        LocalDateTime endtDate = LocalDateTimeUtil.of(deviceDataPageReqDto.getEndTime());

        deviceDataPageReqDto.setStartDate(startDate);
        deviceDataPageReqDto.setEndDate(endtDate);
        Page<DeviceDataVo> page = deviceDataMapper.findListByCondition(deviceDataPageReqDto);
        return PageResponse.of(page, DeviceDataVo.class);
    }


    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByDay(String iotId, String functionId, Long startTime, Long endTime) {
        //调用mappe查询数据
        List<DeviceDataGraphVo> deviceDataGraphVoList =
                deviceDataMapper.queryDeviceDataListByDay(iotId,functionId, LocalDateTimeUtil.of(startTime),LocalDateTimeUtil.of(endTime));
        //
        Map<String,Double> map=null;
        if(CollectionUtil.isNotEmpty(deviceDataGraphVoList)){
            map= deviceDataGraphVoList.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));
        }
        //
        List<DeviceDataGraphVo> deviceDataGraphVo24List = DeviceDataGraphVo.dayInstance(LocalDateTimeUtil.of(startTime));

        //
        for (DeviceDataGraphVo deviceDataGraphVo : deviceDataGraphVo24List) {
            Double value = map.get(deviceDataGraphVo.getDateTime());
            if (value != null){
                deviceDataGraphVo.setDataValue(value);
            }
        }
        return deviceDataGraphVo24List;
    }

    @Override
    public Object queryDeviceDataListByWeek(String iotId, String functionId, Long startTime, Long endTime) {
        //调用mappe查询数据
        List<DeviceDataGraphVo> deviceDataGraphVoList =
                deviceDataMapper.queryDeviceDataListByWeek(iotId,functionId, LocalDateTimeUtil.of(startTime),LocalDateTimeUtil.of(endTime));
        //
        Map<String,Double> map=null;
        if(CollectionUtil.isNotEmpty(deviceDataGraphVoList)){
            map= deviceDataGraphVoList.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));
        }
        //
        List<DeviceDataGraphVo> deviceDataGraphVo24List = DeviceDataGraphVo.weekInstance(LocalDateTimeUtil.of(startTime));

        //
        for (DeviceDataGraphVo deviceDataGraphVo : deviceDataGraphVo24List) {
            Double value = map.get(deviceDataGraphVo.getDateTime());
            if (value != null){
                deviceDataGraphVo.setDataValue(value);
            }
        }
        return deviceDataGraphVo24List;
    }
}
